/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package rowcounter;

import java.sql.*;
import java.io.File;
import java.io.FileInputStream;
import java.util.Properties;

//import java.util.concurrent.ScheduledThreadPoolExecutor;

/**
 *
 * @author raj
 */
public class Main {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        Connection conn;
        Statement stmtTablesList;
        ResultSet rsetTables;
        long loop_start = 0;
        long loop_end=0;
        long queryTotals=0;

        Properties config;
        ArgChecker argChecker = new ArgChecker(args);

        if ( ! argChecker.getValue("ConfigFile").isEmpty()) {
            config=readPropertiesFile(argChecker.getValue("ConfigFile"));
        }

        try {
            Class.forName("oracle.jdbc.driver.OracleDriver");

            String sqlTablesList = "select owner, table_name from all_tables";

            conn = DriverManager.getConnection(
                    "jdbc:oracle:thin:@cdm_test:1521:XE", "hr", "hr");
            // @machineName:port:SID, userid, password
            stmtTablesList = conn.createStatement();
            rsetTables = stmtTablesList.executeQuery(sqlTablesList);

            loop_start=System.currentTimeMillis();
            while (rsetTables.next()) {
                Table currTable = new Table(rsetTables.getString("owner")+"."+rsetTables.getString("table_name"), conn);
                currTable.runQuery();
                System.out.println(currTable.getName()+" = "+currTable.getRowCount()+ " ("+currTable.getQueryTime()+" ms)");
                queryTotals+=currTable.getQueryTime();
            }
            loop_end=System.currentTimeMillis();

            System.out.println("\n\n");
            System.out.println("Total of queryTime values="+queryTotals+" ms");
            System.out.println("Loop runtime = "+(loop_end-loop_start)+" ms");
            System.out.println("\n\n");

            rsetTables.close();
            stmtTablesList.close();
            conn.close();
        }
        catch (Exception e) {
            e.printStackTrace();
        }
        finally {
        }
    }

    public static Properties readPropertiesFile(String filePath) {
       Properties configProps=new Properties();
       // check for ConfigFile variable
       if ( filePath.length()>0 ) {
          File cfgFile = new File(filePath);
          if ( cfgFile.exists() && cfgFile.canRead() && cfgFile.length()>0 ) {
             // load configuration information
             configProps = new Properties();
             try {
                // create and load default properties
                FileInputStream ins = new FileInputStream(filePath);
                configProps.load(ins);
                ins.close();
             }
             catch (Exception e) {
                e.printStackTrace();
             }
          }
       }
       return configProps;
   }

}
